package com.kkm.controller;

import com.kkm.pojo.*;
import com.kkm.service.Imp.SongListServiceImp;
import com.kkm.service.SongListService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.text.ParseException;
import java.util.Map;

@WebServlet("/songList")
public class SongListServlet extends BaseServlet {
     SongListService service=new SongListServiceImp();

    //展示订单

    public void showAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String currentPage=req.getParameter("currentPage");
        if(currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        int rows=8;
        // req.removeAttribute("currPageMax");
        PageBean<SongList> pageBeanOrder=service.selectAll(currentPage,rows);
        req.setAttribute("SongList",pageBeanOrder);
        req.getRequestDispatcher("/showAllSongList.jsp").forward(req,resp);
    }
    //模糊查询订单
    public void checkSongList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //获取查询条件，封装成集合
        Map<String, String[]> condition = req.getParameterMap();
        for(String key : condition.keySet()){
            System.out.println("歌单："+key+" "+condition.get(key)[0]);
        }
        String currentPage=req.getParameter("currentPage");

        int rows=8;
//        req.removeAttribute("currPageMax");
        PageBean<SongList> songList=service.checkSongList(condition,currentPage,rows);
        if(songList==null){
//            userList=service.showUser(currentPage,rows); 如果需要在查询不到时显示全部数据
            req.setAttribute("SongList",songList);
            req.getRequestDispatcher("/showAllSongList.jsp").forward(req,resp);
        }else {
            req.setAttribute("SongList",songList);
            req.getRequestDispatcher("/showAllSongList.jsp").forward(req,resp);
        }

    }
    //删除订单
    public void deleteSongList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int sid = Integer.parseInt(req.getParameter("sid"));
        service.deleteSongList(sid);

        String currentPage=req.getParameter("currentPage");
        int rows=8;
        PageBean<SongList> pageBeanOrder=service.selectAll(currentPage,rows);
        req.setAttribute("SongList",pageBeanOrder);
        req.getRequestDispatcher("/showAllSongList.jsp").forward(req,resp);
    }

public void addGoods(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {

    req.setCharacterEncoding("utf-8");
    resp.setContentType("text/html;charset=utf-8");


    String username = req.getParameter("username");
    String file = req.getParameter("file");
    String introduce = req.getParameter("introduce");
    String style = req.getParameter("style");

    service.addSongList(username,file,introduce,style);

    String currentPage=req.getParameter("currentPage");
    int rows=8;
    PageBean<SongList> pageBeanUser=service.selectAll(currentPage,rows);
    req.setAttribute("Goods",pageBeanUser);
    req.getRequestDispatcher("/showAllSongList.jsp").forward(req,resp);
}

//显示歌单具体歌曲
    public void checkSongById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ParseException {
        String slid = req.getParameter("slid");
        SongList songList=new SongList();
        songList.setId(Integer.parseInt(slid));
        String currentPage = req.getParameter("currentPage");
        if (currentPage == null || "".equals(currentPage)) {
            currentPage = "1";
        }
        int rows = 8;
        // req.removeAttribute("currPageMax");
        PageBean<Song> pageBeanMusic = service.showAllMusic(slid,currentPage, rows);
        if(pageBeanMusic==null){

            req.setAttribute("SongList", songList);
            req.setAttribute("Music", pageBeanMusic);
//            req.getRequestDispatcher("/showMusicListSong.jsp").forward(req, resp);
            resp.getWriter().write("<script language='javascript'>alert('该歌单暂无歌曲！');" +
                    "window.location.href='/showMusicListSong.jsp';</script>");
        }else {
            req.setAttribute("SongList", songList);
            req.setAttribute("Music", pageBeanMusic);
            req.getRequestDispatcher("/showMusicListSong.jsp").forward(req, resp);
        }

    }

    //
}
